﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace SimpleNotify.PushClient.Views
{
    public partial class MainView : Form, IMainView
    {
        private MainViewPresenter _presenter;
        bool _itemCheckEventInProgress = false;

        public MainView()
        {
            _presenter = new MainViewPresenter(this);
            InitializeComponent();
          }

        #region Private Methods

        private void MainView_Shown(object sender, EventArgs e)
        {
            Application.DoEvents();
            _presenter.OnViewReady();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            btnNotifySelected.Enabled = false;
            _presenter.OnRefreshClicked();
            Cursor.Current = Cursors.Default;
        }

        private void btnNotify_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            _presenter.OnNotifyClicked(txtTitle.Text, txtText.Text);
            Cursor.Current = Cursors.Default;
        }

        private void listBoxSubscribers_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (_itemCheckEventInProgress) return;

            if (e.NewValue == CheckState.Checked)
            {
                _itemCheckEventInProgress = true;
                listBoxSubscribers.SetItemChecked(e.Index, true);
                _itemCheckEventInProgress = false;
            }
            else
            {
                _itemCheckEventInProgress = true;
                listBoxSubscribers.SetItemChecked(e.Index, false);
                _itemCheckEventInProgress = false;
            }
            RefreshNotifyButtonState();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < listBoxSubscribers.Items.Count; i++)
            {
                _itemCheckEventInProgress = true;
                listBoxSubscribers.SetItemChecked(i, chkbxAll.Checked);
                _itemCheckEventInProgress = false;
            }
            RefreshNotifyButtonState();
        }

        private void rdbtn_Click(object sender, EventArgs e)
        {
            RadioButton btn = (RadioButton)sender;
            _presenter.OnTypeChanged(btn.Name, btn.Checked);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _presenter.OnInfoClicked();
        }

        #endregion

        #region Interface Members

        public void RefreshNotifyButtonState()
        {
            if (listBoxSubscribers.CheckedItems.Count > 0)
            {
                btnNotifySelected.Enabled = true;
            }
            else
            {
                btnNotifySelected.Enabled = false;
            }
        }

        public void BindData(BindingList<string> data)
        {
            listBoxSubscribers.DataSource = data;
            panel1.Visible = true;
        }

        public List<string> SelectedSubscribers
        {
            get
            {
                List<string> subscribers = new List<string>();

                foreach (var item in listBoxSubscribers.CheckedItems)
                {
                    subscribers.Add(item.ToString());
                }

                return subscribers;
            }
        }

        public void DisplayError(string text)
        {
            MessageBox.Show(this, text, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void DisplayWarning(string text)
        {
            MessageBox.Show(this, text, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        #endregion
    }
}
